import * as server from "@minecraft/server";
import * as data_item from "../data/item";
import * as realm_property from "../system/realm_property";
import * as derived_tools from "../system/derived_tools";
import * as native_tools from "../system/native_tools";
import * as achieve_plan from "../system/achieve_plan";
import * as math_tools from "../system/math_tools";
export function Solidify(object) {
    if (!realm_property.ExpendEnergy(object, -35))
        return;
    const type = derived_tools.AnalysisWeight(data_item.solidify_output).output;
    const item = new server.ItemStack(type, 1);
    const container = object.below()?.getComponent('minecraft:inventory')?.container;
    if (!container || container.emptySlotsCount == 0)
        native_tools.TrySpawnItem(object.dimension, item, math_tools.Vector.add(object, { x: 0.5, y: 1, z: 0.5 }));
    else
        container.addItem(item);
}
;
export function Destroy(object, type) {
    if (!realm_property.ExpendEnergy(object, -15))
        return;
    const locationGroup = [];
    switch (type) {
        case 'east':
            locationGroup.push(math_tools.Vector.add(object, math_tools.Vector.CONSTANT_EAST), math_tools.Vector.add(object, { x: 6, y: 0, z: 0 }));
            break;
        case 'west':
            locationGroup.push(math_tools.Vector.add(object, math_tools.Vector.CONSTANT_WEST), math_tools.Vector.add(object, { x: -6, y: 0, z: 0 }));
            break;
        case 'up':
            locationGroup.push(math_tools.Vector.add(object, math_tools.Vector.CONSTANT_UP), math_tools.Vector.add(object, { x: 0, y: 6, z: 0 }));
            break;
        case 'down':
            locationGroup.push(math_tools.Vector.add(object, math_tools.Vector.CONSTANT_DOWN), math_tools.Vector.add(object, { x: 0, y: -6, z: 0 }));
            break;
        case 'south':
            locationGroup.push(math_tools.Vector.add(object, math_tools.Vector.CONSTANT_SOUTH), math_tools.Vector.add(object, { x: 0, y: 0, z: 6 }));
            break;
        case 'north':
            locationGroup.push(math_tools.Vector.add(object, math_tools.Vector.CONSTANT_NORTH), math_tools.Vector.add(object, { x: 0, y: 0, z: -6 }));
            break;
    }
    ;
    const TickEvent = (args) => {
        args.dimension.runCommand(`fill ${args.location.x} ${args.location.y} ${args.location.z} ${args.location.x} ${args.location.y} ${args.location.z} air [] destroy`);
        return true;
    };
    achieve_plan.PathExecute.Create('破坏核心-射线动画', 1, {
        particles: ['constant:track_rune_red'],
        offset: { x: 0.5, y: 0.5, z: 0.5 },
        location_group: locationGroup,
        dimension: object.dimension,
        on_move: TickEvent,
        cooldown: 1,
        speed: 1
    });
}
;
export function Mine(object) {
    if (!realm_property.ExpendEnergy(object, -150))
        return;
    const current = `mineral_vein•${object.dimension.id}•${Math.floor(object.location.x / 16)}•0•${Math.floor(object.location.z / 16)}`;
    const realmName = realm_property.RealmPropertyName(object, current.split(/•/)[0], 2);
    if (!realmName)
        return realm_property.CreateMineralVein(current);
    const container = object.south()?.getComponent('minecraft:inventory')?.container;
    const itemStack = new server.ItemStack(realm_property.ExpendMineralVein(realmName), 1);
    if (!container || container.emptySlotsCount == 0)
        native_tools.TrySpawnItem(object.dimension, itemStack, math_tools.Vector.add(object, { x: 0.5, y: 1, z: 0.5 }));
    else
        container.addItem(itemStack);
    derived_tools.AlterMessageNotify('<§l§b 虚岩矿脉 §r>§n 剩余矿石§r', object, realm_property.QueryMineralVein(realmName));
}
;
export function Forming(object) {
    if (!realm_property.ExpendEnergy(object, -15))
        return;
    const container = object.above()?.getComponent('minecraft:inventory')?.container;
    if (!container || container.emptySlotsCount == 0)
        return derived_tools.ErrorMessage('<§l§b 金属锻压 §r>§4 发生错误§r', object, { text: '未能在设备上方找到合适的<§l§3 方块容器 §r>' });
    for (let index = 0; index < container.size; index++) {
        const item = container.getItem(index);
        if (!item || item?.amount < 4)
            continue;
        const tags = item?.getTags().filter(tag => tag.startsWith('data_tag:mineral_resources.make'));
        if (tags.length == 0)
            continue;
        const type = tags[0].split('.')[2];
        const stage = item.typeId.split(':')[1].split('.').length;
        const name = stage == 1 ? item.typeId.split(':')[1] : item.typeId.split(':')[1].split('.')[1];
        if (item.amount > 4) {
            item.amount -= 4;
            container.setItem(index, item);
        }
        else
            container.setItem(index);
        container.addItem(new server.ItemStack('item_mineral:' + type + '.' + name));
        server.world.playSound('random.anvil_use', object.location, { volume: 1, pitch: 1 });
        break;
    }
}
